package com.hyy.demo.parallel;

import org.squirrelframework.foundation.fsm.StateMachineBuilderFactory;
import org.squirrelframework.foundation.fsm.UntypedStateMachine;
import org.squirrelframework.foundation.fsm.UntypedStateMachineBuilder;
import org.squirrelframework.foundation.fsm.annotation.StateMachineParameters;
import org.squirrelframework.foundation.fsm.impl.AbstractUntypedStateMachine;

//@States({
//@State(name="Root", entryCallMethod="enterRoot", exitCallMethod="exitRoot", compositeType= StateCompositeType.PARALLEL),
//@State(parent="Root", name="RegionState1", entryCallMethod="enterRegionState1", exitCallMethod="exitRegionState1"),
//@State(parent="Root", name="RegionState2", entryCallMethod="enterRegionState2", exitCallMethod="exitRegionState2")
//})
@StateMachineParameters(stateType=MyState.class, eventType=MyEvent.class, contextType=Integer.class)
public class ParallelStateMachineSample extends AbstractUntypedStateMachine {

    protected void onRoot(String from, String to, MyEvent event, Integer context) {
        System.out.println("进入状态 \'" + to + "\'.");
    }

    public static void main(String[] args) {
        UntypedStateMachineBuilder builder = StateMachineBuilderFactory.create(
                ParallelStateMachineSample.class);
        builder.onEntry(MyState.Root).callMethod("onRoot");

        //定义一个平行状态序列
        builder.defineParallelStatesOn(MyState.Root, MyState.RegionState1, MyState.RegionState2);

        //定义一个顺序状态序列
        builder.defineSequentialStatesOn(MyState.RegionState1, MyState.State11, MyState.State12);
        builder.externalTransition().from(MyState.State11).to(MyState.State12).on(MyEvent.Event1);

        builder.defineSequentialStatesOn(MyState.RegionState2, MyState.State21, MyState.State22, MyState.State23);
        builder.externalTransition().from(MyState.State21).to(MyState.State22).on(MyEvent.Event2);
        builder.externalTransition().from(MyState.State22).to(MyState.State23).on(MyEvent.Event3);

        builder.externalTransition().from(MyState.State12).to(MyState.Root1).on(MyEvent.Event4);
        builder.externalTransition().from(MyState.State23).to(MyState.Root1).on(MyEvent.Event5);
        UntypedStateMachine stateMachine = builder.newStateMachine(MyState.Root);
        stateMachine.start();
        System.out.println(stateMachine.getCurrentState());
        System.out.println(stateMachine.getSubStatesOn(MyState.Root));// 11 21
        stateMachine.fire(MyEvent.Event1);
        System.out.println(stateMachine.getSubStatesOn(MyState.Root));// 21 12
        stateMachine.fire(MyEvent.Event2);
        System.out.println(stateMachine.getSubStatesOn(MyState.Root));// 12 22
        stateMachine.fire(MyEvent.Event3);
        System.out.println(stateMachine.getSubStatesOn(MyState.Root));// 23 12
        System.out.println(stateMachine.getCurrentState());
    }
}
